package com.tianze.tzkf.function.home;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tianze.tzkf.MainActivity;
import com.tianze.tzkf.R;
import com.tianze.tzkf.base.BaseActivity;
import com.tianze.tzkf.function.aboutUs.AboutUsActivity;
import com.tianze.tzkf.function.aboutUs.AboutUsFragment;
import com.tianze.tzkf.function.hardwareInfo.HardWareActivity;
import com.tianze.tzkf.function.hardwareInfo.HardWareFragment;
import com.tianze.tzkf.function.leadRecord.LeadRecordActivity;
import com.tianze.tzkf.function.leadRecord.LeadRecordFragment;
import com.tianze.tzkf.function.systemLayout.SystemLayoutActivity;
import com.tianze.tzkf.function.systemLayout.SystemLayoutFragment;
import com.tianze.tzkf.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.content.Context.ACTIVITY_SERVICE;

/**
 * Created by Zcyang on 2018/12/27.
 */
public class HomeMenuFragment extends Fragment implements AdapterView.OnItemClickListener {

    BaseActivity activity = (BaseActivity) getActivity();

    @BindView(R.id.lv_home_menu)
    ListView lvHomeMenu;
    @BindView(R.id.tv_exit)
    TextView tvExit;
    Unbinder unbinder;
    /**
     * 菜单界面中只包含了一个ListView。
     */
    private ListView menuList;

    /**
     * ListView的适配器。
     */
    private HomeMenuAdapter adapter;


    /**
     * 是否是双页模式。如果一个Activity中包含了两个Fragment，就是双页模式。
     */
    private boolean isTwoPane;
    private List<Fragment> fragmentList;


    public static HomeMenuFragment newInstance() {

        Bundle args = new Bundle();

        HomeMenuFragment fragment = new HomeMenuFragment();
        fragment.setArguments(args);
        return fragment;
    }


    /**
     * 当Activity和Fragment建立关联时，初始化适配器中的数据。
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        adapter = new HomeMenuAdapter(getContext());

    }

    /**
     * 加载menu_fragment布局文件，为ListView绑定了适配器，并设置了监听事件。
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_menu, container, false);
        menuList = (ListView) view.findViewById(R.id.lv_home_menu);
        menuList.setAdapter(adapter);
        menuList.setOnItemClickListener(this);
        unbinder = ButterKnife.bind(this, view);

        Fragment LeadRecordFragment = new LeadRecordFragment();
        Fragment SystemLayoutFragment = new SystemLayoutFragment();
        Fragment HardWareFragment = new HardWareFragment();
        Fragment AboutUsFragment = new AboutUsFragment();

        fragmentList = new ArrayList<>();

        fragmentList.add(LeadRecordFragment);
        fragmentList.add(SystemLayoutFragment);
        fragmentList.add(HardWareFragment);
        fragmentList.add(AboutUsFragment);

        return view;
    }

    /**
     * 当Activity创建完毕后，尝试获取一下布局文件中是否有details_layout这个元素，如果有说明当前
     * 是双页模式，如果没有说明当前是单页模式。
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getActivity().findViewById(R.id.fl_home_detail) != null) {
            isTwoPane = true;
            adapter.setSelectedPosition(0);
            getFragmentManager().beginTransaction().replace(R.id.fl_home_detail, fragmentList.get(0)).commit();
        } else {
            isTwoPane = false;
        }
    }

    /**
     * 处理ListView的点击事件，会根据当前是否是双页模式进行判断。如果是双页模式，则会动态添加Fragment。
     * 如果不是双页模式，则会打开新的Activity。
     */
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {


        if (isTwoPane) {
            adapter.setSelectedPosition(index);
            adapter.notifyDataSetInvalidated();
                FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
                mFragmentTransaction.replace(R.id.fl_home_detail, fragmentList.get(index)).commit();
        } else {
            Intent intent = null;
            if (index == 0) {
                intent = new Intent(getActivity(), LeadRecordActivity.class);
            } else if (index == 1) {
                intent = new Intent(getActivity(), SystemLayoutActivity.class);
            } else if (index == 2) {
                intent = new Intent(getActivity(), HardWareActivity.class);
            } else if (index == 3) {
                intent = new Intent(getActivity(), AboutUsActivity.class);
            }
            startActivity(intent);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_exit)
    public void onViewClicked() {
        ToastUtils.showToast(getContext(), "点击了退出");
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
            activity = (BaseActivity) getActivity();
            activity.removeCurrentActivity();
            activity.goToActivity(MainActivity.class,null);
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            System.exit(0);
        } else {// android2.1
            ActivityManager am = (ActivityManager)activity. getSystemService(ACTIVITY_SERVICE);
            am.restartPackage(activity.getPackageName());
        }
    }
}
